import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Storage } from '@ionic/storage'
import { ConfigsProvider } from '../configs/configs'

import { HTTP } from '@ionic-native/http'

@Injectable()
export class HttpServicesProvider {
  constructor(
    public httpclient: HttpClient,
    public configs: ConfigsProvider,
    public nativeHttp: HTTP,
    private local_storage: Storage
  ) {}

  /**
   * 请求数据
   * @param reqUrl 请求URL
   * @param jsonDatas 传参
   * @param httpHeaders 头信息
   * @param callback 回调方法
   */
  // 登录接口
  requestDataLoginPost(reqUrl, jsonDatas, httpHeaders, callback) {
    reqUrl = this.configs.API_URL + reqUrl
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    }
    if (httpHeaders) {
      httpOptions.headers = httpHeaders
    }

    this.httpclient.post(reqUrl, jsonDatas, httpOptions).subscribe(
      function(data) {
        callback(true, data)
      },
      function(err) {
        callback(false, err)
      }
    )
  }
  // post 请求接口
  requestDataPost(reqUrl, jsonDatas, httpHeaders, callback) {
    this.local_storage.get('token').then(value => {
      if (value) {
        reqUrl = this.configs.API_URL + reqUrl
        if (this.configs.PRODUCT_HTTP) {
          this.nativeHttp.setCookie(this.configs.API_URL, 'token=' + value)

          this.nativeHttp.setHeader(
            this.configs.API_URL,
            'content-type',
            'application/json;charset=UTF-8'
          )

          this.nativeHttp
            .post(reqUrl, jsonDatas, httpHeaders)
            .then(response => {
              try {
                response.data = JSON.parse(response.data)
                callback(true, response.data)
              } catch (e) {
                response.status = 400
                callback(false, 'JSON parsing error')
                console.error('JSON parsing error')
              }
            })
            .catch(error => {})
        } else {
          const httpOptions = {
            headers: {
              token: value
            }
          }
          if (httpHeaders) {
            httpOptions.headers = httpHeaders
          }
          // let data = new FormData()
          let data = {}
          if (jsonDatas) {
            for (var key in jsonDatas) {
              // data.append(key, jsonDatas[key])
              data[key] = jsonDatas[key]
            }
          }
          this.httpclient
            .post(reqUrl, data, {
              headers: {
                'Content-Type': 'application/json',
                token: value
              }
            })
            .subscribe(
              function(data) {
                callback(true, data)
              },
              function(err) {
                callback(false, err)
              }
            )
        }
      }
    })
  }

  /**
   *
   * @param reqUrl
   * @param jsonDatas
   * @param httpHeaders
   * @param callback
   */
  // get请求接口
  requestDataGet(reqUrl, jsonDatas, httpHeaders, callback) {
    this.local_storage.get('token').then(value => {
      if (value) {
        reqUrl = this.configs.API_URL + reqUrl
        if (this.configs.PRODUCT_HTTP) {
          var parms = {}
          var headers = {}
          if (jsonDatas) {
            parms = jsonDatas
          }
          if (httpHeaders) {
            headers = httpHeaders
          }
          if (value) {
            this.nativeHttp.setCookie(this.configs.API_URL, 'token=' + value)
          }
          this.nativeHttp.get(reqUrl, parms, headers).then(
            response => {
              try {
                response.data = JSON.parse(response.data)
                callback(true, response.data)
              } catch (e) {
                response.status = 400
                callback(false, 'JSON parsing error')
                console.error('JSON parsing error')
              }
            },
            error => {
              callback(false, error.error)
            }
          )
        } else {
          const httpOptions = {
            headers: new HttpHeaders({
              token: value
            })
          }

          // if (httpHeaders) {
          //   httpOptions.headers = new HttpHeaders(httpHeaders)
          // }
          if (jsonDatas) {
            httpOptions['params'] = jsonDatas
          }

          this.httpclient.get(reqUrl, httpOptions).subscribe(
            function(data) {
              callback(true, data)
            },
            function(err) {
              callback(false, err)
            }
          )
        }
      }
    })
  }
  // 退出登录接口
  requestDataLogoutGet(callback) {
    this.local_storage.get('token').then(value => {
      if (value) {
        const reqUrl = this.configs.API_URL + `sys/logout`
        if (this.configs.PRODUCT_HTTP) {
          if (value) {
            this.nativeHttp.setCookie(this.configs.API_URL, 'token=' + value)
          }
          this.nativeHttp.get(reqUrl, {}, {}).then(
            response => {
              try {
                response.data = JSON.parse(response.data)
                callback(true, response.data)
              } catch (e) {
                response.status = 400
                callback(false, 'JSON parsing error')
                console.error('JSON parsing error')
              }
            },
            error => {
              callback(false, error.error)
            }
          )
        } else {
          const httpOptions = {
            headers: new HttpHeaders({
              'Content-Type': 'application/json',
              token: value
            })
          }

          this.httpclient.post(reqUrl, null, httpOptions).subscribe(
            function(data) {
              callback(true, data)
            },
            function(err) {
              callback(false, err)
            }
          )
        }
      }
    })
  }
}
